//
//  Pay4GDBlock.m
//  丁诚昊的外包项目(2016_V7宝)
//
//  Created by DimChum_Haul on 16/3/27.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "Masonry.h"
#import "Pay4GDBlock.h"

#import "TTagButton.h" ///子模块:【TTagButton】
#import "UIView+Geometry.h" /// 导入分类：831Kit
#import "UIView+PhyMoVal.h"
#import "UIColor+ColorFullTemplate.h"

/**
 * 是否需要用到Method Swizzling
 * 动态地在TTagButton运行期间动态绑定给一个操作block指针到分类 这是一个问题
 */
#import <objc/objc-runtime.h>

#define NumberOfButtonsCounts 5

/**
 * 2.按钮创建模式为block依赖注入(IOC)而不是iOS7默认的TargetAction模型 不需要在创建按钮之后 绑定一个self.selector方法指针到按钮 而是直接在block中编写按钮响应事件 且按钮的响应事件不与按钮调用者Callee产生任何逻辑关系
 */
@interface Pay4GDBlock ()
@property(strong,nonatomic,readonly) UIButton* selectingButton;
@property (nonatomic) NSMutableArray<__kindof UIButton*>* LinkedList;
@end

@implementation Pay4GDBlock

/** 
 * Issues：TODO
 * 问题的关键其实是在这个方法的重写里 因为我使用了Masonry(AutoLayOut 因为抓不住他的Frame
 * AutoLayout默认就在屏幕的左上角 CGRectZero。。。。没有办法再用Rect判断用户点击了哪个子按钮
 */
- (instancetype)init{
    if (self = [super init]) {
        self.statusEnumType = netStatusInit;
        /// 1.整个[购物车工具栏] 使用iOS7扁平化风格【毛玻璃(高斯模糊)】做容器
        UIView* contentView = [UIView DCH_addBlurView:CGRectZero
                                             blurType:UIBlurEffectStyleLight];
        [self addSubview:contentView];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
            [UIView simplifyWithCornerRadius:contentView with:10.f];
        }];
        
        /// --------------------------------  左边按钮1：联系卖家 --------------------------------
        TTagButton* left1 = [[TTagButton alloc] initWithIcon:@"rightGoods" title:@"联系卖家" ActionBlock:^(TTagButton *sender, netStatus statusEnum) { ///1.绑定事件源
            [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([obj isKindOfClass:[UIImageView class]]){
                    CGAffineTransform rotation2D = CGAffineTransformMakeRotation(M_PI);
                    NSLog(@"按钮网络状态--->%zd",statusEnum);
                    [UIView animateWithDuration:0.33f animations:^{
                        obj.transform = rotation2D;
                    }completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.33f/2 animations:^{
                            obj.transform = CGAffineTransformIdentity;
                        }];
                    }];
                }
            }];
        }];

        TTagButton* left2 = [[TTagButton alloc]initWithIcon:@"高" title:@"收藏商品" ActionBlock:^(TTagButton *sender, netStatus statusEnum) {
            [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([obj isKindOfClass:[UIImageView class]]){
                    CATransform3D rotation3D = CATransform3DMakeRotation(M_PI_2, 0, -1, 0);
                    [UIView animateWithDuration:0.33f animations:^{
                        obj.layer.transform = rotation3D;
                    }completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.33f/2 animations:^{
                            obj.layer.transform = CATransform3DIdentity;
                        }];
                    }];
                }
            }];
        }];
        
        TTagButton* left3 = [[TTagButton alloc]initWithIcon:@"shopping_cart" title:@"购物车" ActionBlock:^(TTagButton *sender, netStatus statusEnum) {
            [UIView TinyShake:sender duration:0.77f shakeTimes:8];
        }];
        [self.LinkedList addObjectsFromArray:@[left1,left2,left3]];/// 加入全局数组队列 方便做级联操作 比如选中其他 其他改变选中状态
        
        //2.添加并布局UI
        [contentView addSubview:left1];
        [contentView addSubview:left2];
        [contentView addSubview:left3];
        /// 一种奇怪的Masonry 刷UI布局的方式 😄
        [left1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.centerY.equalTo(contentView);
            make.width.equalTo(contentView).dividedBy(6);
            make.height.equalTo(contentView);
            make.height.equalTo(left1.mas_width).priorityLow();
        }];
        [left2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.baseline.equalTo(left1);
            make.left.equalTo(left1.mas_right);
            make.centerY.equalTo(left1);
        }];
        [left3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.baseline.equalTo(left1);
            make.left.equalTo(left2.mas_right);
            make.centerY.equalTo(left1);
        }];
        
        /// --  按钮3：加入购物车 在这里调用[iOS本地缓存[购物车记录]]本地API 并提交到服务器用户购物车API --
        TTagButton* add2Car = [[TTagButton alloc]initSimpleTTagBtnWithBackColor:[UIColor Heavy_ColorfulDay]
                                                                      TitleText:@"加入购物车"
                                                                    ActionBlock:nil];
        TTagButton* JustBuyBuyBuy = [[TTagButton alloc]initSimpleTTagBtnWithBackColor:[UIColor GLColor] TitleText:@"直接购买" ActionBlock:^(TTagButton *sender, netStatus statusEnum) {
            NSLog(@"注意上面一个按钮没有传入 但不会蹦 哈哈");
        }];
        [contentView addSubview:JustBuyBuyBuy];
        [contentView addSubview:add2Car];
        [add2Car mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(left3.mas_right).offset(1);
            make.right.equalTo(JustBuyBuyBuy.mas_left).offset(-1);
            make.height.equalTo(contentView);
            make.centerY.equalTo(left1);
        }];
        [JustBuyBuyBuy mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.and.centerY.and.height.equalTo(contentView);
            make.width.equalTo(add2Car);
        }];
        return self;
    }
    return nil;
}

- (NSMutableArray<UIButton *> *)LinkedList{
    if (!_LinkedList) {
        _LinkedList = @[].mutableCopy;
    }
    return _LinkedList;
}
@end


